package com.cqeec.bootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqeec.bootdemo.common.CustomException;
import com.cqeec.bootdemo.entity.Category;
import com.cqeec.bootdemo.entity.Dish;
import com.cqeec.bootdemo.entity.Setmeal;
import com.cqeec.bootdemo.mapper.CategoryMapper;
import com.cqeec.bootdemo.service.CategoryService;
import com.cqeec.bootdemo.service.DishService;
import com.cqeec.bootdemo.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setMealService;
    @Override
    public void removeWithCategory(Long id) {
        //如果当前分类下有菜品，不能删除
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        long count = dishService.count(wrapper);
        if (count > 0) {
            throw new CustomException("当前分类下有菜品，不能删除");
        }
        //如果当前分类下有套餐，不能删除
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId, id);
        long count1 = setMealService.count(wrapper1);
        if (count1 > 0) {
            throw new CustomException("当前分类下有套餐，不能删除");
        }
        //可以删除
        this.removeById(id);
    }
}
